//
//  ILGUtils.m
//  HuaRong
//
//  Created by Happy on 8/31/16.
//  Copyright © 2016 Happy. All rights reserved.
//

#import "ILGUtils.h"

#import <AVFoundation/AVFoundation.h>

@implementation ILGUtils

#pragma mark -  处理富文本
+ (NSAttributedString *)textAttributedString:(NSString *)text color:(UIColor *)color {
    NSDictionary *attribs = @{NSForegroundColorAttributeName:color};
    
    NSMutableAttributedString *strM = [[NSMutableAttributedString alloc] initWithString:text attributes:attribs];
    
    return strM;
}
+ (NSAttributedString *)imageAttributedString:(NSString *)imageName bounds:(CGRect)bounds {
    NSTextAttachment *attachMent = [[NSTextAttachment alloc] init];
    // 设置图片
    attachMent.image = [UIImage imageNamed:imageName];
    // 设置大小
    attachMent.bounds = bounds;
    // 添加
    // 2.使用附件创建属性字符串
    NSAttributedString *attrString = [NSAttributedString attributedStringWithAttachment:attachMent];
    
    return attrString;
}
+ (NSAttributedString *)attributedStr:(NSArray *)textArr color:(NSArray *)colorArr {
    NSMutableAttributedString *strM = [[NSMutableAttributedString alloc] initWithString:@""];
    
    for (int index = 0; index < textArr.count; index++) {
        UIColor *color = [UIColor whiteColor];
        if (colorArr) {
            color = colorArr[index];
        }
        NSDictionary *attribs = @{NSForegroundColorAttributeName:color};
        
        NSString *text = textArr[index];
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:text attributes:attribs];
        
        [strM appendAttributedString:str];
    }
    
    return strM;
}

+ (NSString *)appVersionInfo {
    NSDictionary *plistDic = [[NSBundle mainBundle] infoDictionary];
    NSString *currentBuild = plistDic[@"CFBundleVersion"];
    NSString *currentVersion = plistDic[@"CFBundleShortVersionString"];
    NSString *bundleID = plistDic[@"CFBundleIdentifier"];
    
    NSString *environment = @"发布";
#if DEBUG
    environment = @"开发";
#endif
    
    NSString *serverEnvironment = @"线下";
#if ILGServerEnvironment
    serverEnvironment = @"线上";
#endif
    
    NSString *timeStr = @"未知";
    NSDictionary *versionDic = [[NSUserDefaults standardUserDefaults] objectForKey:@"IWHVersionInfo"];
    if (versionDic) {
        NSString *updateTime = versionDic[@"updateTime"];
        if (updateTime.length) {
            timeStr = [updateTime ilg_timeWithFormat:@"yyyy-MM-dd HH:mm:ss"];
        }
    }
    
    NSString *str = [NSString stringWithFormat:@"Version: %@; Build: %@ \n BundleID: %@ \n 构建环境:%@; 服务器环境:%@ \n 更新时间: %@", currentVersion, currentBuild, bundleID, environment, serverEnvironment, timeStr];
    return str;
}
+ (void)saveAppVersionInfo {
    NSDictionary *versionDic = [[NSUserDefaults standardUserDefaults] objectForKey:@"IWHVersionInfo"];
    NSDictionary *plistDic = [[NSBundle mainBundle] infoDictionary];
    NSString *currentVersion = plistDic[@"CFBundleShortVersionString"];
    NSString *currentBuild = plistDic[@"CFBundleVersion"];
    
    BOOL isNeedUpdate = NO;
    if (versionDic) {
        BOOL isUpdatedForShortVersion = [versionDic[@"shortVersionString"] compare:currentVersion options:NSCaseInsensitiveSearch] == NSOrderedSame;
        BOOL isUpdatedForVersion = [versionDic[@"versionString"] compare:currentBuild options:NSCaseInsensitiveSearch] == NSOrderedSame;
        if (!isUpdatedForShortVersion || !isUpdatedForVersion) {
            isNeedUpdate = YES;
        }
    } else {
        isNeedUpdate = YES;
    }
    
    if (isNeedUpdate) {
        NSMutableDictionary *versionInfoTmp = @{}.mutableCopy;
        versionInfoTmp[@"versionString"] = currentBuild;
        versionInfoTmp[@"shortVersionString"] = currentVersion;
        versionInfoTmp[@"updateTime"] = [NSString stringWithFormat:@"%ld", (long)([[NSDate date] timeIntervalSince1970])];
        [[NSUserDefaults standardUserDefaults] setObject:versionInfoTmp forKey:@"IWHVersionInfo"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

+ (NSString *)getLaunchImageName {
    CGFloat maxImageHeight = 0;
    CGSize viewSize = [UIScreen mainScreen].bounds.size;
    
    NSString *viewOrientation = @"Portrait";
    NSString *launchImageName = nil;
    NSArray* imagesDict = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchImages"];
    for (NSDictionary* dict in imagesDict)
    {
        CGSize imageSize = CGSizeFromString(dict[@"UILaunchImageSize"]);
        if (CGSizeEqualToSize(imageSize, viewSize) && [viewOrientation isEqualToString:dict[@"UILaunchImageOrientation"]])
        {
            launchImageName = dict[@"UILaunchImageName"];
            break; //找到匹配的就结束查找
        }
        
        // 如果最后没找到匹配的，就取其中最高的
        if (imageSize.height > maxImageHeight) {
            maxImageHeight = imageSize.height;
            launchImageName = dict[@"UILaunchImageName"];
        }
    }
    return launchImageName;
}

+ (void)openSettingsForURLString:(NSString *)urlString
{
    // iOS10也可以使用url2访问，不过使用url1更好一些，可具体根据业务需求自行选择
    if (@available(iOS 11.0, *)) {
        NSURL *url2 = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        if ([[UIApplication sharedApplication] canOpenURL:url2]){
            [[UIApplication sharedApplication] openURL:url2 options:@{} completionHandler:nil];
        }
    } else {
        NSURL *url1 = [NSURL URLWithString:@"App-Prefs:root=NOTIFICATIONS_ID"];
        if ([[UIApplication sharedApplication] canOpenURL:url1]){
            if (@available(iOS 10.0, *)) {
                [[UIApplication sharedApplication] openURL:url1 options:@{} completionHandler:nil];
            } else {
                [[UIApplication sharedApplication] openURL:url1];
            }
        }
    }
}

@end
